package com.union.core.job.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.hnjowe.webdb.DBOperation;
import com.union.core.job.dao.DailyJobAudiDao;
import com.union.core.job.model.DailyJobAudi;
import com.union.core.sys.service.SysService;

public class DailyJobAudiDaoImpl implements DailyJobAudiDao {
	private static Logger log = Logger.getLogger(DailyJobAudiDaoImpl.class
			.getName());
	private DBOperation opera;
	private SysService sysService = SysService.getInstance();
	@Override
	public boolean addJob(DailyJobAudi jobAudi) {
		boolean result = false;
		try {
			opera = new DBOperation();
			opera.transcationBegin();
			StringBuffer sqlBuf = new StringBuffer();
			sqlBuf.append("insert into job_sentiment_audi");
			sqlBuf.append("(job_id,audi_details,status,");
			sqlBuf.append("job_file,audi_time,staff_id,organization_id,update_staff,update_time,organization_name,staff_name,file_path) ");
			sqlBuf.append("values(?,?,?,?,SYSDATE(),?,?,?,SYSDATE(),?,?,?);");
			opera.prepStatment(sqlBuf.toString());
			opera.setInt(1, jobAudi.getJob_id());	
			opera.setString(2, jobAudi.getAudi_details());
			opera.setInt(3, jobAudi.getStatus());
			opera.setString(4, jobAudi.getJob_file());
			opera.setInt(5, jobAudi.getStaff_id());
			opera.setInt(6, sysService.getSessionListener().getPERSONNEL().getOrganization_id());
			opera.setString(7, sysService.getSessionListener().getPERSONNEL().getPersonnel_name());
			opera.setString(8, jobAudi.getOrganization_name());
			opera.setString(9, jobAudi.getStaff_name());
			opera.setString(10, jobAudi.getFile_path());
			opera.execute();
			result = opera.transcationCommit();
			if(result == false)
			{
				log.error("sql="+opera.getPres().toString());
				return false; 
			}
		} catch (Exception e) {
			log.error(e);
			log.error("sql="+opera.getPres().toString());
		} finally {
			opera.closePres();
			opera.closeConn();
			log.error("sql="+opera.getPres().toString());
		}
		return result; 
	}

	@Override
	public boolean editJob(DailyJobAudi jobAudi) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public List<DailyJobAudi> getJobList(int jobId) {
		List<DailyJobAudi> dailyJobAudiList = new ArrayList<DailyJobAudi>();
		try{
			opera = new DBOperation();
			opera.transcationBegin();
			StringBuffer sql = new  StringBuffer();
			sql.append("select audi_id,job_id,audi_details,status,audi_time,");
			sql.append("staff_id,job_file,update_staff,update_time,organization_id,organization_name,staff_name,file_path ");
			sql.append(" from job_sentiment_audi");
			sql.append(" where job_id = ? ");
			sql.append(" order by audi_time desc ");
			opera.prepStatment(sql.toString());
			opera.setInt(1, jobId);
			ResultSet res = opera.executeQuery();
			int i =0; 
			while (res.next()) {
				DailyJobAudi  dailyJobAudi= new DailyJobAudi();
				dailyJobAudi.setAudi_id(res.getInt(1));
				dailyJobAudi.setJob_id(res.getInt(2));
				dailyJobAudi.setAudi_details(res.getString(3));
				dailyJobAudi.setStatus(res.getInt(4));
				dailyJobAudi.setAudi_time(res.getString(5));
				dailyJobAudi.setStaff_id(res.getInt(6));
				dailyJobAudi.setJob_file(res.getString(7));
				dailyJobAudi.setUpdate_staff(res.getString(8));
				dailyJobAudi.setUpdate_time(res.getString(9));
				dailyJobAudi.setOrganization_id(res.getInt(10));
				dailyJobAudi.setOrganization_name(res.getString(11));
				dailyJobAudi.setStaff_name(res.getString(12));
				dailyJobAudi.setFile_path(res.getString(13));
				dailyJobAudiList.add(i,dailyJobAudi);
				i++; 
			}
		} catch (Exception e) {
			log.error(e);
		} finally {
			opera.closePres();
			opera.closeConn();
		}
		return dailyJobAudiList;
	}

	@Override
	public int getJobListCount(int jobId) {
		// TODO Auto-generated method stub
		return 0;
	}

}
